/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Class corresponding to the "profiles" element of the profiles_config.xml file.
 *
 */
public class JBProfiles implements Serializable
{
    /** List of single user types profiles (JBProfile). */
    private Map profilesUserMap = new HashMap();

    /** List of static group(s) typed profiles (JBProfile). */
    private Map profilesStaticGroupMap = new HashMap();

    /** List of dynamic group typed profiles (JBProfile). */
    private Map profilesDynamicGroupMap = new HashMap();

    /** List of filter typed profiles (JBProfile). */
    private Map profilesFilterMap = new HashMap();

    /** List of default typed profiles (JBProfile). */
    private Map profilesDefaultMap = new HashMap();

    /** List of global resources (JBRessource). */
    private Map ressourcesMap = new HashMap();

//DW/2618/BeginPatch
    /** Static group resource definition. */
    private JBGroupDef staticGroup;

    /** Dynamic group resource definition. */
    private JBGroupDef dynamicGroup;
//DW/2618/EndPatch

    /**
     * Adds a single user profile in list.
     *
     * @param jbProfile profile to add
     */
    public void addProfileUser(JBProfile jbProfile)
    {
        jbProfile.setType(JBProfile.TYPE_USER);
        profilesUserMap.put(jbProfile.getName().trim().toLowerCase(), jbProfile);
    }

    /**
     * Adds a static group(s) profile in list.
     *
     * @param jbProfile profile to add
     */
    public void addProfileStaticGroup(JBProfile jbProfile)
    {
        jbProfile.setType(JBProfile.TYPE_STATIC_GROUP);
        profilesStaticGroupMap.put(jbProfile.getName().trim().toLowerCase(),
            jbProfile);
    }

    /**
     * Adds a dynamic group profile in list.
     *
     * @param jbProfile profile to add
     */
    public void addProfileDynamicGroup(JBProfile jbProfile)
    {
        jbProfile.setType(JBProfile.TYPE_DYNAMIC_GROUP);
        profilesDynamicGroupMap.put(jbProfile.getName().trim().toLowerCase(),
            jbProfile);
    }

    /**
     * Adds a filter profile in list.
     *
     * @param jbProfile profile to add
     */
    public void addProfileFilter(JBProfile jbProfile)
    {
        jbProfile.setType(JBProfile.TYPE_FILTER);
        profilesFilterMap.put(jbProfile.getName().trim().toLowerCase(),
            jbProfile);
    }

    /**
     * Adds a default profile in list.
     *
     * @param jbProfile profile to add
     */
    public void addProfileDefault(JBProfile jbProfile)
    {
        jbProfile.setType(JBProfile.TYPE_DEFAULT);
        profilesDefaultMap.put(jbProfile.getName().trim().toLowerCase(),
            jbProfile);
    }

    /**
     * Gets a global resource from its name.
     *
     * @param name name of the resource
     *
     * @return resource or null
     */
    public JBRessource findRessourceByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBRessource) ressourcesMap.get(key);
    }

    /**
     * Adds a resource in list.
     *
     * @param jbRessource resource to add
     */
    public void addRessource(JBRessource jbRessource)
    {
        ressourcesMap.put(jbRessource.getName().trim().toLowerCase(),
            jbRessource);
    }

    /**
     * Gets a single user profile from its name.
     *
     * @param name profile name
     *
     * @return profile or null
     */
    public JBProfile findProfileUserByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBProfile) profilesUserMap.get(key);
    }

    /**
    * Gets a static group(s) profile from its name.
    *
    * @param name profile name
    *
    * @return profile or null
    */
    public JBProfile findProfileStaticGroupByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBProfile) profilesStaticGroupMap.get(key);
    }

    /**
     * Gets a dynamic group profile from its name.
     *
     * @param name profile name
     *
     * @return profile or null
     */
    public JBProfile findProfileDynamicGroupByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBProfile) profilesDynamicGroupMap.get(key);
    }

    /**
    * Gets a filter profile from its name.
    *
    * @param name profile name
    *
    * @return profile or null
    */
    public JBProfile findProfileFilterByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBProfile) profilesFilterMap.get(key);
    }

    /**
     * Gets a default profile from its name.
     *
     * @param name profile name
     *
     * @return profile or null
     */
    public JBProfile findProfileDefaultByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBProfile) profilesDefaultMap.get(key);
    }

    /**
     * Displays the resources and profiles configuration.
     *
     * @return Resources and profiles configuration.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        if (ressourcesMap != null)
        {
            buf.append("Map components of ressourcesMap = ");
            buf.append(System.getProperty("line.separator"));
            it = ressourcesMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (profilesUserMap != null)
        {
            buf.append("Map components of profilesUserMap = ");
            buf.append(System.getProperty("line.separator"));
            it = profilesUserMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (profilesStaticGroupMap != null)
        {
            buf.append("Map components of profilesStaticGroupMap = ");
            buf.append(System.getProperty("line.separator"));
            it = profilesStaticGroupMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (profilesDynamicGroupMap != null)
        {
            buf.append("Map components of profilesDynamicGroupMap = ");
            buf.append(System.getProperty("line.separator"));
            it = profilesDynamicGroupMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (profilesFilterMap != null)
        {
            buf.append("Map components of profilesFilterMap = ");
            buf.append(System.getProperty("line.separator"));
            it = profilesFilterMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (profilesDefaultMap != null)
        {
            buf.append("Map components of profilesDefaultMap = ");
            buf.append(System.getProperty("line.separator"));
            it = profilesDefaultMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Gets the global resources.
     * @return resources table
     */
    public Map getRessourcesMap()
    {
        return ressourcesMap;
    }

    /**
     * Gets the default profiles.
     * @return profiles table
     */
    public Map getProfilesDefaultMap()
    {
        return profilesDefaultMap;
    }

    /**
     * Gets the dynamic group profiles.
     * @return profiles table
     */
    public Map getProfilesDynamicGroupMap()
    {
        return profilesDynamicGroupMap;
    }

    /**
     * Gets the filter profiles.
     * @return profiles table
     */
    public Map getProfilesFilterMap()
    {
        return profilesFilterMap;
    }

    /**
     * Gets the static group(s) profiles.
     * @return profiles table
     */
    public Map getProfilesStaticGroupMap()
    {
        return profilesStaticGroupMap;
    }

    /**
     * Gets the single user profiles.
     * @return profiles table
     */
    public Map getProfilesUserMap()
    {
        return profilesUserMap;
    }

    /**
     * Adds gloabl resources in each profile.
     * Should be run when all resources and profiles are loaded.
     */
    public void enrichProfileResources()
    {
        Iterator itRess = ressourcesMap.keySet().iterator();
        JBRessource globalResource = null;
        String globalResourceName = null;

        while (itRess.hasNext())
        {
            globalResourceName = (String) itRess.next();
            globalResource = (JBRessource) ressourcesMap.get(globalResourceName);
            enrichAprofileType(profilesUserMap, globalResourceName,
                globalResource);
            enrichAprofileType(profilesStaticGroupMap, globalResourceName,
                globalResource);
            enrichAprofileType(profilesDynamicGroupMap, globalResourceName,
                globalResource);
            enrichAprofileType(profilesFilterMap, globalResourceName,
                globalResource);
            enrichAprofileType(profilesDefaultMap, globalResourceName,
                globalResource);
        }
    }

    /**
     * Adds a global resource in each profile of given type.
     *
     * @param profiles list of profiles to enrich
     * @param globalResourceName global resource name
     * @param globalResource global rensource
     */
    private void enrichAprofileType(Map profiles, String globalResourceName,
        JBRessource globalResource)
    {
        Iterator itProf = profiles.values().iterator();
        JBProfile jbProfile = null;
        JBRessources jbRessources = null;

        while (itProf.hasNext())
        {
            jbProfile = (JBProfile) itProf.next();
            jbRessources = jbProfile.getJbRessources();

            // check if the resource is redefined in this profile
            if ((jbRessources != null) &&
                    (jbRessources.findRessourceByName(globalResourceName) == null))
            {
                jbRessources.addRessource(globalResource);
            }
        }
    }

//DW/2618/BeginPatch
    /**
     * Sets static group resource definition.
     * @param value static group resource definition
     */
    public void setStaticGroup(JBGroupDef value)
    {
        staticGroup = value;
    }

    /**
     * Gets static group resource definition.
     * @return static group resource definition
     */
    public JBGroupDef getStaticGroup()
    {
        return staticGroup;
    }

    /**
     * Sets dynamic group resource definition.
     * @param value dynamic group resource definition
     */
    public void setDynamicGroup(JBGroupDef value)
    {
        dynamicGroup = value;
    }

    /**
     * Gets dynamic group resource definition.
     * @return dynamic group resource definition
     */
    public JBGroupDef getDynamicGroup()
    {
        return dynamicGroup;
    }
//DW/2618/EndPatch

}
